from chapter_indices import old_testament, new_testament


def __chapter_generator__(chapter_full_title, chapter_index, capacity, backward=False):
    '''
    :param start: 从哪一篇的哪一章开始
    :param capacity: 每日读经的章数
    '''

    # 选择新约或旧约
    # 利用python中and和or的特性实现另类的三元表达式(and后必须是隐式True)
    bible_indices = old_testament.get(
        chapter_full_title) and old_testament or new_testament

    chapters_amount = bible_indices[chapter_full_title][1]
    if chapter_index > chapters_amount:
        chapter_index = chapters_amount

    # 储存每日读经的章节数
    chapter_list = []
    index_list = []
    count = 0
    locate_start_point = 1

    while True:
        # 在整部旧约/新约中循环"卷"
        # 例如在新约, 从马太福音循环到启示录
        bible_indices_k = reversed(
            bible_indices.keys()) if backward else bible_indices.keys()
        for chapter in bible_indices_k:
            # 未get将报错
            chapter_short, num = bible_indices.get(chapter)

            # 在"卷"中循环"章"
            # 例如在马太福音, 从1章循环到28章
            start = num if backward else 1
            stop = 0 if backward else num+1
            step = -1 if backward else 1
            for index in range(start, stop, step):

                # 之所以有locate_start_point是因为程序需要一个"起始章节"(循环起点).
                # 例如从马太福音第10章开始循环
                if locate_start_point:

                    # 如果当前"卷"不匹配给定值, 终止当前循环, 继续匹配下一卷
                    # 例如给定值是"马太福音", 当前"卷"不是"马太福音"直接终止"章"的循环
                    if chapter != chapter_full_title:
                        break

                    # "卷"匹配后, 继续判断当前"章"是否匹配给定值.
                    # 如果匹配, 则确定循环起点, 程序向前执行
                    elif index == chapter_index:
                        locate_start_point = 0
                    else:
                        continue

                index_list.append(index)

                # 定义count变量而不是len(index_list), 是因为很有可能产生多个index_list
                count += 1
                if count == capacity:
                    chapter_list.append((chapter, chapter_short, index_list))
                    yield chapter_list
                    index_list = []
                    chapter_list = []
                    count = 0

            # "卷"的"章"尽数录入index_list, 但是还没有达到capacity
            # 如果不提交, 这一卷的章会被录入到下一卷中
            # 例如, 期待chapter_dict = {'太':[26,27,28], '可':[1,2]}
            # 结果是chapter_dict = {'可':[26,27,28,1,2]}
            if index_list:
                chapter_list.append((chapter, chapter_short, index_list))
                index_list = []


def get_chapter_info(chapter_full_title='创世记', chapter_index=1, capacity=2, days=7, *, skip: int = 0, raw_data=False):

    if skip:
        if backward := skip < 0:
            skip = abs(skip)
            backward = True
        g = __chapter_generator__(
            chapter_full_title, chapter_index, capacity, backward=backward)
        for i in range(skip+1):
            temp_data = next(g)

        title = temp_data[0][0]
        index = temp_data[0][2][0]
        return title, index
        # return get_chapter_info(title, index, capacity, days)
    else:
        g = __chapter_generator__(chapter_full_title, chapter_index, capacity)

        return [next(g) for i in range(days)]


def fmt_to_str(chapter):
    return ', '.join(f'{title}{num[0]}~{num[-1]}' if len(num) > 1 else f'{title}{num[0]}' for _, title, num in chapter)


if __name__ == '__main__':
    # 这种在整部新约/旧约循环的方法有一个缺点
    # 如果每天的读经数过多, 直接导致类似死循环的内存压力
    # 不过GUI设定的capacity最多为5
    g1 = get_chapter_info('加拉太书', 1, 2)
    g3 = get_chapter_info('玛拉基书', 1, 1, skip=507)
    g4 = get_chapter_info('创世记', 1, 2, skip=-7)
    print(g1)
    print(g3)
    print(g4)
